<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠
// +----------------------------------------------------------------------

namespace app\edu\model;

use think\Model;

/**
 * 教师模型
 * @author 李忠
 * @createAt 2018-11-08 14:49:00
 */
class Teacher extends Model {
    // 设置当前模型对应的完整数据表名称
    protected $table = '__EDU_TEACHER__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //数据自动完成
    protected $auto = ['update_by' => UID];
    protected $insert = ['create_by' => UID];

    //修改器
    public function getStatusTextAttr($value, $data) {
        //01:退休
        //03:死亡
        //04:返聘
        //07:离职
        //18:在岗
        //19:试用
        //20:废止
        $status = $data['status'];
        $html = null;
        $config = config('teacher_status');
        if (isset($config[$status])) {

        }
        switch ($status) {
            case '18':
                $html = '<span class="label label-success label-badge">' . $config[$status] . '</span>';
                break;
            case '19':
                $html = '<span class="label label-info label-badge">' . $config[$status] . '</span>';
                break;
            case '07':
                $html = '<span class="label label-danger label-badge">' . $config[$status] . '</span>';
                break;
            default:
                $text = $status;
                if (isset($config[$status])) {
                    $text = $config[$status];
                }
                $html = '<span class="label label-default label-badge">' . $text . '</span>';
                break;
        }
        return $html;

    }

    public function getTypeTextAttr($value, $data) {
        $type = $data['type'];
        $config = config('teacher_type');
        if ($config[$type]) {
            return $config[$type];
        } else {
            return $type;
        }

    }

    //自定义方法
    public static function lists($status, $office_id, $type, $title) {
        $map = array();
        if ($status !== null && $status !== '') {
            array_push($map, ['a.status', '=', $status]);
        }
        if ($office_id !== null && $office_id !== '') {
            array_push($map, ['a.office_id', '=', $office_id]);
        }
        if ($type !== null && $type !== '') {
            array_push($map, ['a.type', '=', $type]);
        }

        if ($title !== '' && !empty($title)) {
            array_push($map, ['a.name|title', 'like', '%' . $title . '%']);
        }
        return self::alias('a')
            ->join('__SYS_OFFICE__ b', 'a.office_id=b.id')
            ->where($map)->field('a.*, b.title as office_text')->order('a.id desc')->paginate();
    }

    public static function state($id, $status) {
        return self::where('id', $id)->update(['status' => $status]);
    }
}
